using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections.Generic;
using System.Collections;

namespace WX.WMS.Common
{
    /// <summary>
    /// Paging control
    /// </summary>
    [ToolboxData("<{0}:Pager PageClause=\"<%$ Resources:GlobalResource,lblPageClause %>\" PageSize=\"<%# ConfigurationInfo.PageSize %>\" RecordClause=\"<%$ Resources:GlobalResource,lblRecordClause %>\" FirstClause=\"<%$ Resources:GlobalResource,lblFirstPage %>\" LastClause=\"<%$ Resources:GlobalResource,lblLastPage %>\" PreviousClause=\"<%$ Resources:GlobalResource,lblPrevious %>\" NextClause=\"<%$ Resources:GlobalResource,lblNext %>\"  runat=\"server\"> </{0}:Pager>")]
    public class Pager : WebControl, IPostBackEventHandler, INamingContainer
    {

        private string rePostBack = "Page$";
        #region call back event
        private static readonly object EventCommand = new object();

        public event CommandEventHandler Command
        {
            add { Events.AddHandler(EventCommand, value); }
            remove { Events.RemoveHandler(EventCommand, value); }
        }

        protected virtual void OnCommand(CommandEventArgs e)
        {
            CommandEventHandler clickHandler = (CommandEventHandler)Events[EventCommand];
            if (clickHandler != null) clickHandler(this, e);
        }

        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            string temp = eventArgument.Substring(eventArgument.IndexOf("$")+1);
            OnCommand(new CommandEventArgs(this.UniqueID, Convert.ToInt32(temp)));
        }
        #endregion

        #region paging settings
       // private double itemCount; // total record number
        private int pageSize = 15; // paging number
       // private int pageCount; // paging total number

        [Browsable(false)]
        public double ItemCount
        {
            get {
                if (ViewState["aspnetPagerItemCount"] == null)
                {
                    ViewState["aspnetPagerItemCount"] = 0;
                    return 0;
                }
                else
                {
                    return Convert.ToDouble(ViewState["aspnetPagerItemCount"]);
                }
            }
            set
            {
                
                ViewState["aspnetPagerItemCount"] = value;
               // itemCount =  Convert.ToDouble(ViewState["aspnetPagerItemCount"]);
                double divide = ItemCount / PageSize;
                double ceiled = System.Math.Ceiling(divide);
                PageCount = Convert.ToInt32(ceiled);
            }
        }

        [Browsable(false)]
        public int CurrentIndex
        {
            get
            {
                if (ViewState["aspnetPagerCurrentIndex"] == null)
                {
                    ViewState["aspnetPagerCurrentIndex"] = 1;
                    return 1;
                }
                else
                {
                    return Convert.ToInt32(ViewState["aspnetPagerCurrentIndex"]);
                }
            }
            set { ViewState["aspnetPagerCurrentIndex"] = value; }
        }

        [Browsable(false)]
        public int PageIndex
        {
            get
            {
                if (ViewState["aspnetPagerCurrentIndex"] == null)
                {
                    ViewState["aspnetPagerCurrentIndex"] = 0;
                    return 1;
                }
                else
                {
                    return Convert.ToInt32(ViewState["aspnetPagerCurrentIndex"])-1;
                }
            }
        }

        [Category("PagerSetting"),
         Description("GetThePageSize")]
        public int PageSize
        {
            get
            {
                if (ViewState["aspnetPagerPageSize"] == null)
                {
                    ViewState["aspnetPagerPageSize"] = 15;
                    return 15;
                }
                else
                {
                    return Convert.ToInt32(ViewState["aspnetPagerPageSize"]);
                }
            }
            set { ViewState["aspnetPagerPageSize"] = value; }
        }

        [Browsable(false)]
        public int PageCount
        {
            get
            {
                if (ViewState["aspnetPageCount"] == null)
                {
                    ViewState["aspnetPageCount"] = 0;
                    return 1;
                }
                else
                {
                    return Convert.ToInt32(ViewState["aspnetPageCount"]);
                }
            }
            set { ViewState["aspnetPageCount"] = value; }
        }
        #endregion

        #region showing mesage settings
        private string records = @"{0} records per page, Total {1} records,";
        private string pages = @"Page {0} Of {1},";
        private string first = @"First";
        private string last = @"Last";
        private string previous = @"Previous";
        private string next = @"Next";

        [Category("paging seetings"), Description("the outputting styple of the current page and total page")]
        public string PageClause
        {
            get { return pages; }
            set { pages = value; }
        }
        [Category("paging seetings"), Description("the outputting style of the page number and total record number")]
        public string RecordClause
        {
            get { return records; }
            set { records = value; }
        }

        [Category("paging seetings"), Description("link symbol of the last page")]
        public string LastClause
        {
            get { return last; }
            set { last = value; }
        }

        [Category("paging seetings"), Description("link symbol of the first page")]
        public string FirstClause
        {
            get { return first; }
            set { first = value; }
        }

        [Category("paging seetings"), Description("link symbol of the previous page")]
        public string PreviousClause
        {
            get { return previous; }
            set { previous = value; }
        }

        [Category("paging seetings"), Description("link symbol of the next page")]
        public string NextClause
        {
            get { return next; }
            set { next = value; }
        }

        #endregion

        #region
        private string GetPostBack(Control control, string arg)
        {
                //string js = "javascript:__doPostBack('{0}','{1}')";
                //return string.Format(js, control.UniqueID, arg);
           return Page.ClientScript.GetPostBackClientHyperlink(control, arg);

            
        }
        #endregion
        #region outputting define
        // get the paging message
        private string GetAlternativeText(int index)
        {
            return string.Format(" title=\"{0}\"", index);
        }
        //first page's link
        private string RenderFirst()
        {
            string templateCell = "<td class=\"PagerOtherPageCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\"" + " " + FirstClause + " " + "\"> " + "&nbsp;" + FirstClause + "&nbsp;" + " </a></td>";
            return String.Format(templateCell, GetPostBack(this, rePostBack+"1"));
        }
        //the text of the first page is disable
        private string RenderDisabledFirst()
        {
            string templateCell = "<td>" + "&nbsp;" + FirstClause + "&nbsp;" + "</td>";
            return templateCell;
        }
        //last page's link
        private string RenderLast()
        {
            string templateCell = "<td class=\"PagerOtherPageCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\"" + " " + LastClause + " " + "\"> " + "&nbsp;" + LastClause + "&nbsp;" + " </a></td>";
             return String.Format(templateCell, GetPostBack(this, rePostBack+PageCount.ToString()));
        }
        //the text of the last page is disable
        private string RenderDisabledLast()
        {
            string templateCell = "<td>" + "&nbsp;" + LastClause + "&nbsp;" + "</td>";
            return templateCell;
        }
        //previous page's link
        private string RenderPrevious()
        {
            string templateCell = "<td class=\"PagerOtherPageCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\"" + " " + PreviousClause + " " + (CurrentIndex - 1).ToString() + "\"> " + "&nbsp;" + PreviousClause + "&nbsp;" + " </a></td>";
            return String.Format(templateCell, GetPostBack(this, rePostBack+(CurrentIndex - 1).ToString()));
        }
        //the text of previous page is disable
        private string RenderDisabledPrevious()
        {
            string templateCell = "<td>" + "&nbsp;" + PreviousClause + "&nbsp;" + "</td>";
            return templateCell;
        }
        //next page's link
        private string RenderNext()
        {
            string templateCell = "<td class=\"PagerOtherPageCells\"><a class=\"PagerHyperlinkStyle\" href=\"{0}\" title=\"" + " " + NextClause + " " + (CurrentIndex + 1).ToString() + "\"> " + "&nbsp;" + NextClause + "&nbsp;" + " </a></td>";
            return String.Format(templateCell, GetPostBack(this, rePostBack+(CurrentIndex + 1).ToString()));
        }
        // the text of the next page is disable
        private string RenderDisabledNext()
        {
            string templateCell = "<td>" + "&nbsp;" + NextClause + "&nbsp;" + "</td>";
            return templateCell;
        }
        #endregion

        #region rewrite the outputting method of the control
        protected override void Render(HtmlTextWriter writer)
        {

            if (Page != null) Page.VerifyRenderingInServerForm(this);

            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "3");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "1");
            writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "PagerContainerTable");

            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "PagerInfoCell");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(string.Format(RecordClause, PageSize, ItemCount) + " "
                + string.Format(PageClause, CurrentIndex.ToString(), PageCount.ToString()) + " ");
            writer.RenderEndTag();

            if (CurrentIndex != 1)
                writer.Write(RenderFirst());
            else
                writer.Write(RenderDisabledFirst());

            if (CurrentIndex != 1)
                writer.Write(RenderPrevious());
            else
                writer.Write(RenderDisabledPrevious());

            if (CurrentIndex < PageCount)
                writer.Write(RenderNext());
            else
                writer.Write(RenderDisabledNext());

            if (CurrentIndex < PageCount)
                writer.Write(RenderLast());
            else
                writer.Write(RenderDisabledLast());

            writer.RenderEndTag();

            writer.RenderEndTag();

            base.Render(writer);
        }
        #endregion
    }
}